﻿using Asxsyd92Core.Utils.Provider;
using System;
using System.Linq;
using WebOS.Dal;
using WebOS.Modle;

namespace WebOS.Bll
{
    public class RoleAppBll
    {
        public static RoleAppBll Instance
        {
            get { return SingletonProvider<RoleAppBll>.Instance; }
        }
        public string GetAppList(int role)
        {
            if (role == 110)
            {
                string sql = "select *from RoleApp where ParentID=@ParentID order by Sort";

                var da = xRoleAppDal.Instance.GetList(sql, new { ParentID = Guid.Empty }).ToList();


                return Asxsyd92Core.Utils.JSONhelper.ToJson(new { code = 0, msg = "", count = da.Count == 0 ? 0 : da.First().Count, data = da }, false);
            }
            else
            {
                string sql = "select *from RoleApp RA,( select MenuID,ur.identifier from  UsersRole ur,Role r where  ur.identifier=r.identifier and r.identifier=" + role + ") as tab  where  ParentID='00000000-0000-0000-0000-000000000000' and tab.MenuID=RA.ID  order by Sort";
                var da = RoleAppDal.Instance.GetList(sql, null).ToList();

                return Asxsyd92Core.Utils.JSONhelper.ToJson(new { code = 0, msg = "", count = 0, data = da }, false);
            }

        }
        public string GetAppDektop(Guid Role)
        {
            string sql = "  select *from RoleApp where RoleID IN(  select RoleID from UsersRole WHERE MemberID =@MemberID and ParentID !=@ParentID) and tagtype=1";// "select  *from  RoleApp where RoleID= '" + Role + "' and ParentID in  (select ID from RoleApp where  ParentID = '00000000-0000-0000-0000-000000000000')";

            var da = RoleAppDal.Instance.GetList(sql, new { MemberID = Role, ParentID = Guid.Empty }).ToList();


            return Asxsyd92Core.Utils.JSONhelper.ToJson(new { code = 0, msg = "", count = da.Count == 0 ? 0 : da.First().Count, data = da }, false);
        }
        public string GetAllApp()
        {
            string sql = "select *from RoleApp where ParentID=@ParentID";

            var da = xRoleAppDal.Instance.GetList(sql, new { ParentID = Guid.Empty }).ToList();


            return Asxsyd92Core.Utils.JSONhelper.ToJson(new { code = 0, msg = "", count = da.Count == 0 ? 0 : da.First().Count, data = da }, false);
        }
        public string AddMenu(RoleApp art)
        {
            bool ins = false; var tag = 0;
            ins = art.ID == Guid.Empty ? true : false;
            if (ins)
            {
                art.ID = Guid.NewGuid();
                tag = RoleAppDal.Instance.Insert(art);
            }
            else tag = RoleAppDal.Instance.Update(art);

            if (tag > 0) return Newtonsoft.Json.JsonConvert.SerializeObject(new { msg = "操作成功！", Success = true });
            else return Newtonsoft.Json.JsonConvert.SerializeObject(new { msg = "操作失败！", Success = false });
        }

        public string DelByID(Guid iD)
        {
            var da = RoleAppDal.Instance.GetWhere(new { ID = iD });
            if (da.FirstOrDefault() != null)
            {
                if (RoleAppDal.Instance.Delete(iD) > 0)
                {
                    //删除关系
                    UsersRoleDal.Instance.Delete(new { MenuID = iD });
                   //下拉下的关系
                  var wherews=  RoleAppDal.Instance.GetWhere(new { ParentID = iD });
                  //删除下拉下的菜单  
                    RoleAppDal.Instance.Delete(new { ParentID = iD });
                    //删除下拉项的关系
                    string ids = "";
                    foreach(var item in wherews)
                    {
                        ids += "'" + item.ID + "'" + ",";
                    }
                    if (ids != "") { 
                     UsersRoleDal.Instance.Delete(ids.Substring(0,ids.Length-1));
                    }
                   
                    return Newtonsoft.Json.JsonConvert.SerializeObject(new { msg = "删除成功！", Success = true });
                };
            }
            return Newtonsoft.Json.JsonConvert.SerializeObject(new { msg = "删除失败！", Success = false });

        }

        public string GetSetMenu(int v)
        {
            //得到菜单
            string sql = "select *from RoleApp where ParentID=@ParentID";
            string w = "";
            var da = xRoleAppDal.Instance.GetList(sql, new { ParentID = Guid.Empty }).ToList();
            //去掉主菜单
            if (da != null)
            {
                foreach (var item in da)
                {
                    w += "'" + item.ID + "'" + ",";
                }
                w = w.Substring(0, w.Length - 1);
            }
            var ws = "";
            if (w != "")
            {
                ws = " and MenuID not in(" + w + ")";
            }
            //得到选中
            string sqls = "SELECT * FROM  UsersRole where identifier=" + v + ws;
            var vv = UsersRoleDal.Instance.GetList(sqls, null).ToList();



            return Asxsyd92Core.Utils.JSONhelper.ToJson(new { code = 0, msg = "", count = da.Count == 0 ? 0 : da.First().Count, data = da, s_data = vv }, false);

        }
    }
}
